package com.jjwxc.jwjskandriod.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.jjwxc.jwjskandriod.config.Constants;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.UUID;

public class PhoneInfoUtils {
    private Context context;

    public PhoneInfoUtils(Context context) {
        this.context = context;
    }

    @SuppressLint("HardwareIds")
    public String getIMEI() {
        String id;
        //android.telephony.TelephonyManager
        if (Build.VERSION.SDK_INT > 28) {
            id = getUniqueID();
            return id;
        }

        TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            id = mTelephony.getDeviceId();
        } else {
            //android.provider.Settings;
            id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return id;
    }

    private String getUniqueID() {
        String id = null;
        @SuppressLint("HardwareIds") String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (!StringUtils.isEmpty(androidId) && !androidId.equals("9774d56d682e549c")) {
            try {
                UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf-8"));
                id = uuid.toString();
                return id;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (StringUtils.isEmpty(id)) {
            id = getUUID();
        }

        //使用硬件信息拼湊出來的15位號碼
        return StringUtils.isEmpty(id) ? UUID.randomUUID().toString() : id;
    }

    private String getUUID() {
        String serial = null;
        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 +
                Build.BRAND.length() % 10 +
                Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 +
                Build.HOST.length() % 10 +
                Build.ID.length() % 10 +
                Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10 +
                Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 +
                Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 位

        if (Build.VERSION.SDK_INT <= 29) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    serial = Build.getSerial();
                } else {
                    serial = Build.SERIAL;
                }
                return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
            } catch (Exception e) {
                e.printStackTrace();
                serial = "serial"; // 隨便一個初始化
            }
        } else {
            serial = Build.UNKNOWN; // 隨便一個初始化
        }
        return serial;
    }
}